import {Col, Form, Input, message, Modal, Row, Spin} from "antd";
import React from "react";
import axios from "../../../../components/service/request";

const EditModal = ({isModalVisible, cancelModal, value, reload, baseUir}) => {

  //确认框的加载状态、旋转加载的状态、模块选项的值和模块选项的更新函数 定义初始状态
    const [confirmLoading, setConfirmLoading] = React.useState(false);
    const [spinLoading, setSpinLoading] = React.useState(false);
    const [title, setTitle] = React.useState("新增设备类型");
    const [form] = Form.useForm();

    React.useEffect(() => {
        if (isModalVisible) {
            setSpinLoading(true)
            if (value) {
                setTitle("设备类型信息")
                form.setFieldsValue({
                    id: value.id,
                    classCode: value.classCode,
                    name: value.name,
                    num: value.num, 
                    text:value.text
                });
                    
            }
            setSpinLoading(false)
        }
    }, [form, isModalVisible, value]);

    //编辑时 保存按钮方法
    const handleOk = () => {
        setConfirmLoading(true)
        form.validateFields()
            .then((values) => {
                form.resetFields();
                axios.post(baseUir+"/save", values).then(res => {
                    if (res.code === 200) {
                        message.info(res.msg)
                        cancelModal()
                        reload()
                    }  else {
                        message.info(res.msg)
                    }
                })
            })
        setConfirmLoading(false)
    }
    //编辑时取消按钮方法
    const handleCancel = () => {
        form.resetFields()
        cancelModal()
    }
    //
    return (
       < Modal title={title}
               visible={isModalVisible}
               onOk={handleOk}
               onCancel={handleCancel}
               okText={'保存'}
               confirmLoading={confirmLoading}
               cancelText={'取消'}
               width={800}
        >
            <Spin spinning={spinLoading}>
                <Form form={form}
                      labelAlign={'right'}
                      layout={'vertical'}
                >
                    <Form.Item name="id" hidden={true}>
                        <Input/>
                    </Form.Item>
                    <Row gutter={16}>
                        <Col span={12}>
                            <Form.Item name="classCode" label="设备编号" rules={[{required: true,message: "必须填写设备编号！"}]}>
                                {
                                        <Input/>
                                }
                            </Form.Item>
                        </Col>
                        <Col span={12}>
                            <Form.Item name="name" label="设备类型" rules={[{ required: true, message: "必须填写设备类型！" }]}>
                                {
                                        <Input/>
                                }
                            </Form.Item>
                        </Col>
                    </Row>
                    <Row gutter={16}>
                        <Col span={12}>
                            <Form.Item name="num" label="设备数量">
                                {
                                    value ? <Input disabled={true} /> : <Input disabled={true}/>
                                }
                                
                            </Form.Item>
                        </Col>
                        <Col span={12}>
                            <Form.Item name="text" label="备注">
                                {
                                    <Input/>
                                }
                            </Form.Item>
                        </Col>
                    </Row>
                </Form>
            </Spin>

        </Modal>
    );

}

export default EditModal;
